<?php
/**
 * Create by 11li
 * Date 2020/1/12
 * Time 22:05
 * 支付相关
 */
namespace app\common\service;

use app\common\library\MyWeixin;
use app\common\model\PaymentRecord;

class PaymentService extends BaseService {
	
	/**
	 * 创建支付记录
	 * @param array $orderIds
	 * @param $userId
	 * @param $amount
	 * @return bool|string
	 */
	public function createRecord(array $orderIds, $userId, $amount)
	{
		$model = new PaymentRecord();
		$res = $model->isUpdate(false)
			->save([
				'amount' => $amount,
				'order_id' => implode(',', $orderIds),
				'user_id' => $userId,
			]);
		if($res) {
			return $model->getLastInsID();
		}
		
		return false;
	}
	
	public function initPayment($parmentRecordId, $amount, $notify, $openId) {
		$weixin = MyWeixin::getInstance();
		$paymentRes = $weixin->unifiedOrder($parmentRecordId, $amount, $notify, $openId);
		if($paymentRes['status']) {
			return $paymentRes['data'];
		} else {
			$this->error = $paymentRes['msg'];
			return false;
		}
	}
}
